// =============================================================================
//  Spangles Analyzer
//
//  Copyright(c) 2008
//  See LICENSE.txt for licensing information.
// =============================================================================

package com.google.spangles.view.renderers;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.google.spangles.domain.Color;
import com.google.spangles.domain.Location;
import com.google.spangles.view.Renderer;
import com.google.spangles.view.Visualization;
import com.google.spangles.view.VisualizationElement;

/**
 * (Description)
 * (Any invariants [conditions that are always true] )
 * (Usage instructions, and/or examples)
 * (TODOs, ASSUMPTIONs, etc.)
 * (Known Bugs)
 * (Concurrency strategy - not thread safe or how it's thread safe)
 * 
 * @author Chris
 */
public class ShellRenderer implements Renderer
{
   Map<VisualizationElement, String[]> representations = new HashMap<VisualizationElement, String[]>();
   
   public ShellRenderer()
   {
      representations.put(new VisualizationElement(null, false), new String[]{
                                                                              ".     .",
                                                                              "       ",
                                                                              "       ",
                                                                              "   .   "
       });
      representations.put(new VisualizationElement(null, true), new String[]{
                                                                              "   .   ",
                                                                              "       ",
                                                                              "       ",
                                                                              ".     ."
       });
      representations.put(new VisualizationElement(Color.WHITE, false), new String[]{
                                                                              "+-----+",
                                                                              " \\.../ ",
                                                                              "  \\./  ",
                                                                              "   +   "
       });
      representations.put(new VisualizationElement(Color.WHITE, true), new String[]{
                                                                                    "   +   ",
                                                                                    "  /.\\  ",
                                                                                    " /...\\ ",
                                                                                    "+-----+"
       });
      representations.put(new VisualizationElement(Color.BLACK, false), new String[]{
                                                                                     "+-----+",
                                                                                     " \\###/ ",
                                                                                     "  \\#/  ",
                                                                                     "   +   "
       });
      representations.put(new VisualizationElement(Color.BLACK, true), new String[]{
                                                                                    "   +   ",
                                                                                    "  /#\\  ",
                                                                                    " /###\\ ",
                                                                                    "+-----+"
       });
      representations.put(new VisualizationElement(Color.GRAY, false), new String[]{
                                                                                     "+-----+",
                                                                                     " \\???/ ",
                                                                                     "  \\?/  ",
                                                                                     "   +   "
       });
      representations.put(new VisualizationElement(Color.GRAY, true), new String[]{
                                                                                    "   +   ",
                                                                                    "  /?\\  ",
                                                                                    " /???\\ ",
                                                                                    "+-----+"
       });
      
   }
   
   /* (non-Javadoc)
    * @see com.google.spangles.view.Renderer#render(com.google.spangles.view.Visualization)
    */
   public void render(Visualization v)
   {
      // get rendering dimensions
      int x1 = v.getMinimumX();
      int x2 = v.getMaximumX();
      int xs = x2-x1+1;
      
      int y1 = v.getMinimumY();
      int y2 = v.getMaximumY();
      int ys = y2-y1+1;
    
      ShellBuffer sb = new ShellBuffer(ys*3+4);
      
      // add the vertical ticks
      for(int i=0;i<xs;i++)
      {
         Character c = new Character((char)('A'+i));
         
         sb.render(0, 6+3*i, new String[]{c.toString(),"|"});
      }
      
      for(int i=0;i<ys;i++)
      {
         String toShow = Integer.toString(i+1) + "_";
         
         sb.renderString(4+i*3, 3-toShow.length(), toShow);
      }
      
      Collection<Location> ll = v.locations();
      
      for(Iterator<Location> li = ll.iterator(); li.hasNext(); )
      {
         Location l = li.next();
         int x = l.getX();
         int y = l.getY();
         VisualizationElement ve = v.getElement(l);
         
         String[] representation = representations.get(ve);
         
         sb.render(3*(y-y1)+3, 3*(x-x1)+3, representation);         
      }
      
      sb.showBuffer();
   }
}
